import java.util.*;


public class Leetcode1944 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(canSeePersonsCount(new int[]{10, 6, 8, 5, 11, 9})));
        System.out.println(Arrays.toString(canSeePersonsCount(new int[]{5, 1, 2, 3, 10})));
        System.out.println(Arrays.toString(canSeePersonsCount(new int[]{3, 1, 5, 8, 6})));
    }

    public static int[] canSeePersonsCount(int[] heights) {
        ArrayDeque<Integer> stack = new ArrayDeque<>();

        int[] res = new int[heights.length];
        for (int i = heights.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && stack.peek() < heights[i]) {
                stack.pop();
                ++res[i];
            }

            if (!stack.isEmpty()){
                ++res[i];
            }

            stack.push(heights[i]);
        }


        return res;
    }
}
